<template>
  <div v-loading="loading" class="app-container bg-gray">
    <el-row>
      <el-card class="box-1">
        <el-row class="header">
          <div class="text-center">
            <el-popover
              placement="top-start"
              title="成单率说明：(不包含当天数据)"
              width="400"
              trigger="hover">
              <p>本月成单率: 本月激活客户数 / 本月指派客户数</p>
              <p>总成单率: 总激活客户数 / 总客户数</p>
              <label slot="reference" style="color: black;">{{ userName }} - 业绩运营详情  <i class="el-icon-info"/></label>
            </el-popover>
          </div>
        </el-row>
        <el-row class="row">
          <el-col :span="4">
            <div>今日客户量</div>
            <div>{{ info['today_customer'] || 0 }}</div>
          </el-col>
          <el-col :span="4">
            <div>本月客户量</div>
            <div>{{ info['month_customer'] || 0 }}</div>
          </el-col>
          <!--<el-col :span="4">
            <div>今日业绩</div>
            <div>{{ info['today_bonus_number'] || 0 }}</div>
          </el-col>-->
          <el-col :span="4">
            <div>本月业绩</div>
            <div>{{ info['month_bonus_number'] || 0 }}</div>
          </el-col>
          <el-col :span="4">
            <div>本月成单量</div>
            <div>{{ info['month_member'] || 0 }}</div>
          </el-col>
          <el-col :span="4">
            <div>本月成单率</div>
            <div>{{ info['month_conversion'] || 0 }}%</div>
          </el-col>
          <el-col :span="4">
            <div>本月丢单率</div>
            <div>{{ info['month_no_conversion'] || 0 }}%</div>
          </el-col>
        </el-row>
        <el-row class="row">
          <el-col :span="4">
            <div>昨日客户量</div>
            <div>{{ info['yesterday_customer'] || 0 }}</div>
          </el-col>
          <el-col :span="4">
            <div>上月客户量</div>
            <div>{{ info['last_month_customer'] || 0 }}</div>
          </el-col>
          <!--<el-col :span="4">
            <div>昨日业绩</div>
            <div>{{ info['yesterday_bonus_number'] || 0 }}</div>
          </el-col>-->
          <el-col :span="4">
            <div>上月业绩</div>
            <div>{{ info['last_month_bonus_number'] || 0 }}</div>
          </el-col>
          <el-col :span="4">
            <div>上月成单量</div>
            <div>{{ info['last_month_member'] || 0 }}</div>
          </el-col>
          <el-col :span="4">
            <div>总成单率</div>
            <div>{{ info['conversion'] || 0 }}%</div>
          </el-col>
          <el-col :span="4">
            <div>总丢单率</div>
            <div>{{ info['no_conversion'] || 0 }}%</div>
          </el-col>
        </el-row>

        <el-table :data="info['source']" max-height="250">
          <el-table-column label="不同来源客户概况" header-align="center" align="center">
            <el-table-column label="来源" prop="source_name" header-align="center" align="center" />
            <el-table-column label="本日数量" prop="today_customer" header-align="center" align="center" />
            <el-table-column label="昨日数量" prop="yesterday_customer" header-align="center" align="center" />
            <el-table-column label="本月数量" prop="month_customer" header-align="center" align="center" />
            <el-table-column label="上月数量" prop="last_month_customer" header-align="center" align="center" />
            <el-table-column label="本月成单率" prop="month_conversion" header-align="center" align="center">
              <template slot-scope="scope">
                {{ scope.row['month_conversion'] || 0 }}%
              </template>
            </el-table-column>
            <el-table-column label="本月丢单率" prop="month_no_conversion" header-align="center" align="center">
              <template slot-scope="scope">
                {{ scope.row['month_no_conversion'] || 0 }}%
              </template>
            </el-table-column>
            <el-table-column label="总成单率" prop="conversion" header-align="center" align="center">
              <template slot-scope="scope">
                {{ scope.row['conversion'] || 0 }}%
              </template>
            </el-table-column>
            <el-table-column label="总丢单率" prop="no_conversion" header-align="center" align="center">
              <template slot-scope="scope">
                {{ scope.row['no_conversion'] || 0 }}%
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>
      </el-card>
    </el-row>

    <div class="lineChartBox">
      <el-row :gutter="32">
        <el-col :xs="24" :sm="24" :md="24" :lg="8" :xl="8">
          <el-card>
            <div slot="header" class="text-center">
              <span>客户量 - 曲线图</span>
            </div>
            <line-chart :chart-data="info['customer_list']" />
          </el-card>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="8" :xl="8">
          <el-card>
            <div slot="header" class="text-center">
              <span>业绩 - 曲线图</span>
            </div>
            <line-chart :chart-data="info['customer_number']" />
          </el-card>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="8" :xl="8">
          <el-card>
            <div slot="header" class="text-center">
              <span>成单率/丢单率 - 曲线图（%）</span>
            </div>
            <line-chart :chart-data="info['order_from']" />
          </el-card>
        </el-col>
      </el-row>
    </div>

    <el-row class="box-2">
      <el-col :span="24">
        <el-card>
          <div class="header">
            <div class="title">
              <label style="margin-right: 20px;">业绩订单</label>
              <el-date-picker v-model="dateTime" class="picker" type="month" size="small" value-format="yyyy-MM" placeholder="选择月份" @change="getData" />
              <el-tag type="success">销售提成：{{ total_money }}</el-tag>
              <el-tag type="primary">预期售后奖金：{{ total_bonus_money }}</el-tag>
            </div>
          </div>
          <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border row-class-name="cursor-p" @sort-change="handleSortChange" @cell-click="orderClick">
            <el-table-column prop="id" label="ID" min-width="65" header-align="center" align="center"/>
            <el-table-column prop="member_name" label="客户名称" min-width="120" header-align="center" align="center"/>
            <el-table-column prop="member_phone" label="手机号码" min-width="120" header-align="center" align="center"/>
            <el-table-column prop="goods_name" label="商品名称" min-width="120" header-align="center" align="center"/>
            <el-table-column prop="goods_money" label="商品金额" min-width="110" header-align="center" align="center" sortable />
            <el-table-column prop="buy_time" label="购买时间" min-width="120" header-align="center" align="center" sortable>
              <template slot-scope="scope">
                {{ parseTime(scope.row['buy_time'], '{y}-{m}-{d}') }}
              </template>
            </el-table-column>
            <el-table-column prop="order_bonus" label="订单提成" min-width="110" header-align="center" align="center" sortable>
              <template slot-scope="scope">
                <el-tag type="success" >{{ scope.row.order_bonus }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="remarks" label="备注" min-width="360" header-align="center"/>
            <el-table-column prop="order_status" label="订单状态" min-width="100" header-align="center" align="center">
              <template slot-scope="scope">
                {{ scope.row.status===0?'审核中':scope.row.status===1?'审核成功':scope.row.status===2?'已拒绝':'已退款' }}
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :page-sizes="pageSizes"
            :total="total"
            class="pagination"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"/>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import LineChart from '../components/LineChart'
  import { handleSortChange, handleSizeChange, handleCurrentChange, columnCheck, parseTime } from '@/utils/table'

  export default {
    components: { LineChart },
    data() {
      return {
        loading: false,
        user_id: '',
        dateTime: '',

        info: {
          customer_list: [],
          customer_number: [],
          order_from: []
        },

        // 表格相关数据
        tableMaxHeight: document.documentElement.clientHeight - 380,
        tableLoading: false,
        multipleSelection: [],
        currentPage: 1,
        pageSize: this.$store.getters.userInfo.pageSize,
        pageSizes: [10, 20, 50, 100, 1000],
        total: 0,
        tableData: [],
        filter: {
          type: [{ text: '已完成', value: '已完成' }, { text: '待审核', value: '待审核' }]
        }
      }
    },
    watch: {

    },
    created() {
      this.userName = this.$route.query.name;
      this.user_id = this.$route.query.id;
      this.dateTime = this.$route.query.dateTime;
      this.getData();
      this.getPersonalityData();
    },
    methods: {
      handleCurrentChange, handleSizeChange, handleSortChange, columnCheck, parseTime,
      getData(order) {
        this.tableLoading = true;
        order || (order = {});
        const url = 'achievement/getList' // 获取当月的订单,不能根据时间筛选
        const data = {
          page_no: this.currentPage,
          page_size: this.pageSize,
          dateTime: this.dateTime,
          user_id: this.user_id
        };
        order.sortName && (data.sortName = order.sortName);
        order.sortOrder && (data.sortOrder = order.sortOrder);
        this.$store.dispatch('GetConnect', { url, data }).then(res => {
          this.tableData = res.data.rows
          this.total_money = res.data.total_money;
          this.total_bonus_money = res.data.total_bonus_money;
          this.total = res.data.total
          this.tableLoading = false
        }).catch(e => {
          this.tableLoading = false
          this.$message.error(e.msg + ',请刷新或联系管理员')
        });
      },

      getPersonalityData() {
        this.loading = true;
        const url = 'platform/user';
        const data = { id: this.user_id };
        this.$store.dispatch('GetConnect', { url, data }).then(res => {
          this.loading = false;
          this.info = res.data;
        }).catch(e => {
          this.loading = false;
          this.$message.error(e.msg + ',请刷新或联系管理员');
        })
      },

      filterTag(value, row) {
        // :filters="filter.type" :filter-method="filterTag" filter-placement="bottom-end"
        return row.type === value;
      },
      orderClick(row) {
        this.$router.push({
          path: '/orders/achievement/info',
          query: { id: row.id }
        });
      }
    }
  }
</script>

<style lang="scss" type="text/scss" scoped>
  @import '../../../styles/index.scss';
  .box-1 {
    .header {
      background: #f5f7fa;
      border: 1px solid #ebeef5;
      padding: 10px 0;
      font-size: 14px;
      font-weight: bold;
      color: #909399;
      .info-btn {
        position: absolute;
        top: 9px;
        right: 9px;
      }
    }
    .row {
      border: 1px solid #ebeef5;
      border-top: 0;
      .el-col {
        padding: 8px 0;
        border-right: 1px solid #ebeef5;
        &:last-child {
          border-right: 0;
        }
        > div {
          text-align: center;
          margin: 8px 0;
          font-size: 11px;
          color: #434343;
          &:last-child {
            font-size: 20px;
            margin-top: 10px;
            margin-bottom: 0;
          }
        }
      }
    }
  }

  .lineChartBox {
    padding-top: 20px;
  }

  .box-2 {
    padding-top: 20px;
    .header {
      @include flex-row(center, space-between);
      margin-bottom: 10px;
      .title {
        font-size: 16px;
        font-weight: 600;
        padding: 0 15px;
      }
    }
  }
</style>
